-------------------------------------------------------------------------------------
-- -*- coding: utf-8 -*-
--
-- @author:   zxf
-- @email:    07291@qq.com
-- @date:     2024/5/8
-- @module:   bt_test
-- @describe: 行为树测试
-- @version:  v1.0
--

-------------------------------------------------------------------------------------

local bt_test          = {
    VERSION       = '',
    AUTHOR_NOTE   = '',
    MODULE_NAME   = '行为树',
    SCRIPT_UPDATE = '20250206D',

}

-- 自身模块
local this             = bt_test
local decider = decider
local trace = trace
local main_ctx = main_ctx
-- 设置模块
local settings = settings
local sleep = sleep
---@type bt
local bt = import('bt/bt_framework/bt')
---@type bt_path_res
local bt_path_res = import('game/config/bt_path_res')
---@type MAIN_TARGET_TYPE_CONFIG
local MAIN_TARGET_TYPE_CONFIG = import('bt/custom_node/enum_type_cfg/MAIN_TARGET_TYPE_CONFIG')
---@type owner
local owner = import('game/func/owner')
-------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------
-- 运行前置条件
this.eval_ifs          = {
    -- [启用] 游戏状态列表
    yes_game_state = {}, --login_cfg.STATUS_IN_GAME, login_cfg.STATUS_LOADING_MAP
    -- [禁用] 游戏状态列表
    not_game_state = {},
    -- [启用] 配置开关列表
    yes_config     = {},
    -- [禁用] 配置开关列表
    not_config     = {},
    -- [时间] 模块超时设置(可选)
    -- time_out       = 240,
    -- [其它] 特殊情况才用(可选)
    is_working     = function()
        return true
    end,
    -- [其它] 功能函数条件(可选)
    is_execute     = function()
        return true
    end,
}

-- 轮循函数列表
this.poll_functions    = {}
this.ctx               = {
    time = 0,
    abort = nil,
    gameStatus = nil,
    isClear = nil,
    check_network_time = 800, -- 网络检测的时间 tick 约25分钟
    check_network_timer = 0,  -- 网络检测计时器
    deltaTime = nil,
    setDeltaTime = function(t, deltaTime)
        if deltaTime then
            t.deltaTime = deltaTime
        end
    end
}


------------------------------------------------------------------------------------
-- 预载函数(重载脚本时)
this.super_preload     = function()
    -- 日志记录级别，0=trace, 1=debug, 2=info, 3=warn, 4=error, 5=critical
    settings.log_level = 0
    -- 日志通道，1=调试器, 2=日志文件, 3=调试器和日志文件
    settings.log_type_channel = 3
    trace.log_warn("版本信息：" .. this.SCRIPT_UPDATE)
    bt.set_owner(owner)
    bt.set_strategic_arr(bt_path_res.strategic_res)
    bt.set_subsidiary_tree(bt_path_res.subsidiary_res)
    bt.set_bt_arr(MAIN_TARGET_TYPE_CONFIG.TYPE_OPTIONS)
    local fullPath = main_ctx:c_fz_path() .. [[Script\]]
    bt.load_tree(fullPath)
end

-------------------------------------------------------------------------------------
-- 预载处理
this.preload           = function()
end

-------------------------------------------------------------------------------------
-- 功能入口函数
this.entry             = function()
    local realDeltaTime = 0
    local waitTime = 0
    while decider.is_working()
    do
        math.randomseed(os.time())
        waitTime = bt.run_tree(math.random(1800, 3000), realDeltaTime)
        realDeltaTime = waitTime / 1000
        sleep(waitTime)
    end
    return true
end

-------------------------------------------------------------------------------------
-- 模块超时处理
this.on_timeout        = function()
end

-------------------------------------------------------------------------------------
-- 卸载处理
this.unload = function()
end

-------------------------------------------------------------------------------------
-- 效验登陆异常
this.check_login_error = function()
end

-------------------------------------------------------------------------------------
-- 实例化新对象

function this.__tostring()
    return this.MODULE_NAME
end

this.__index = this

function this:new(args)
    local new = {}

    -- 预载函数(重载脚本时)
    if this.super_preload then
        this.super_preload()
    end

    if args then
        for key, val in pairs(args) do
            new[key] = val
        end
    end

    -- 设置元表
    return setmetatable(new, this)
end

-------------------------------------------------------------------------------------
-- 返回对象
return this:new()

-------------------------------------------------------------------------------------
